package com.growthgate.bootcommon.handler;

import com.growthgate.bootcommon.common.ResponseMsg;
import com.growthgate.bootcommon.common.ResponseResult;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 *
 * @author GrowthGate
 * @date 2020/2/14
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> authorizationException(HttpServletRequest request, @NotNull Exception e) {
        e.printStackTrace();
        // 统一异常处理
        return new ResponseEntity<>(ResponseResult.fail(ResponseMsg.SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}